package com.example.baselibrary.db.dao;

import android.text.TextUtils;

import com.example.baselibrary.db.BTDBHelper;
import com.example.baselibrary.db.table.GroupTable;

import java.sql.SQLException;
import java.util.Collection;
import java.util.Locale;
import cc.vv.lklibrary.db.dao.DBBaseTableDao;
import cc.vv.lklibrary.db.help.DBBaseOpenHelper;
import cc.vv.lklibrary.log.LogOperate;

/**
 * 群组信息
 *
 * @author: NPF
 * @date: 2018/4/10.
 */
public class GroupDao extends DBBaseTableDao<GroupTable, String> {
    public GroupDao(DBBaseOpenHelper openHelper) {
        super(openHelper);
    }

    private static GroupDao mInstance;

    public static GroupDao getInstance() {
        if (null == mInstance) {
            synchronized (GroupDao.class) {
                if (null == mInstance) {
                    mInstance = new GroupDao(BTDBHelper.getInstance());
                }
            }
        }
        return mInstance;
    }

    /**
     * 群组头像昵称更新
     *
     * @param groupId
     * @param groupNick
     * @param groupAvatar
     */
    public int updateNickAndAvatar(String groupId, String groupNick, String groupAvatar) {
        int result = -1;
        try {
            if (TextUtils.isEmpty(groupId)) {
                return result;
            }
            String sql = "UPDATE groupInfo SET groupNick = ?, groupAvatar = ? WHERE groupId = ?";
            result = mDao.updateRaw(sql, null == groupNick ? "" : groupNick, null == groupAvatar ? "" : groupAvatar, groupId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 群组成员数量更新
     *
     * @param groupId
     * @param groupCount
     */
    public int updateCount(String groupId, int groupCount) {
        int result = -1;
        try {
            String sql = "UPDATE groupInfo SET groupCount = ? WHERE groupId = ?";
            result = mDao.updateRaw(sql, String.valueOf(groupCount), groupId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 插入群组信息或者更新头像昵称
     *
     * @param groupTable
     * @return
     */
    public int updateGroup(GroupTable groupTable) {
        int result = -1;
        try {
            GroupTable queryTableBean = mDao.queryForId(groupTable.groupId);
            if (null == queryTableBean) {
                result = mDao.create(groupTable);
            } else {
                updateNickAndAvatar(groupTable.groupId, groupTable.groupNick, groupTable.groupAvatar);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            LogOperate.e(String.format(Locale.CHINESE, "数据添加失败,原因：%s", e.getMessage()));
        }
        return result;
    }

    /**
     * 批量插入
     *
     * @param isClearTable 是否删除表数据
     * @param collection
     * @return
     */
    public int create(boolean isClearTable, Collection<GroupTable> collection) {
        int result = -1;
        try {
            if (isClearTable) {
                mDao.deleteBuilder().delete();
            }
            result = mDao.create(collection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

}
